package com.langsotech.studio.project.wizard.project;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;
public class FileUtil {
	public static void copyDirectory(String sourceDirName,
			String destinationDirName) throws Exception {
		copyDirectory(new File(sourceDirName), new File(destinationDirName));
	}
	public static void copyDirectory(File source, File destination)
			throws Exception {
		if ((source.exists()) && (source.isDirectory())) {
			if (!(destination.exists()))
				destination.mkdirs();
			File[] fileArray = source.listFiles();
			for (int i = 0; i < fileArray.length; ++i)
				if (fileArray[i].isDirectory())
					copyDirectory(fileArray[i], new File(destination.getPath()
							+ File.separator + fileArray[i].getName()));
				else
					copyFile(fileArray[i], new File(destination.getPath()
							+ File.separator + fileArray[i].getName()));
		}
	}
	public static void deltree(String directory) {
		deltree(new File(directory));
	}
	public static void deltree(File directory) {
		if ((directory.exists()) && (directory.isDirectory())) {
			File[] fileArray = directory.listFiles();
			for (int i = 0; i < fileArray.length; ++i) {
				if (fileArray[i].isDirectory())
					deltree(fileArray[i]);
				else
					fileArray[i].delete();
			}
			directory.delete();
		}
	}
	public static String getPath(String fullFileName) {
		int pos = fullFileName.lastIndexOf("/");
		if (pos == -1)
			pos = fullFileName.lastIndexOf("\\");
		String shortFileName = fullFileName.substring(0, pos);
		return shortFileName;
	}
	public static String getShortFileName(String fullFileName) {
		int pos = fullFileName.lastIndexOf("/");
		if (pos == -1)
			pos = fullFileName.lastIndexOf("\\");
		String shortFileName = fullFileName.substring(pos + 1, fullFileName
				.length());
		return shortFileName;
	}
	public static boolean exists(String fileName) {
		File file = new File(fileName);
		return file.exists();
	}
	public static String[] listDirs(String fileName) throws IOException {
		return listDirs(new File(fileName));
	}
	public static String[] listDirs(File file) throws IOException {
		List dirs = new ArrayList();
		File[] fileArray = file.listFiles();
		for (int i = 0; i < fileArray.length; ++i) {
			if (fileArray[i].isDirectory())
				dirs.add(fileArray[i].getName());
		}
		return ((String[]) dirs.toArray(new String[0]));
	}
	public static String[] listFiles(String fileName) throws IOException {
		return listFiles(new File(fileName));
	}
	public static String[] listFiles(File file) throws IOException {
		List files = new ArrayList();
		File[] fileArray = file.listFiles();
		for (int i = 0; i < fileArray.length; ++i) {
			if (fileArray[i].isFile())
				files.add(fileArray[i].getName());
		}
		return ((String[]) files.toArray(new String[0]));
	}
	public static void mkdirs(String pathName) {
		File file = new File(pathName);
		if (!(file.exists()))
			file.mkdirs();
	}
	public static String read(String fileName) throws IOException {
		return read(new File(fileName));
	}
	public static String read(File file) throws IOException {
		BufferedReader br = new BufferedReader(new FileReader(file));
		StringBuffer sb = new StringBuffer();
		for (String line = null; (line = br.readLine()) != null;) {
			sb.append(line).append('\n');
		}
		br.close();
		return sb.toString().trim();
	}
	public static String readByEncoding(String fileName, String encoding)
			throws IOException {
		return readByEncoding(new File(fileName), encoding);
	}
	public static String readByEncoding(File file, String encoding)
			throws IOException {
		InputStreamReader br = new InputStreamReader(new FileInputStream(file),
				encoding);
		FileInputStream fis = new FileInputStream(file);
		char[] strings = new char[fis.available()];
		new StringBuffer();
		br.read(strings);
		br.close();
		String returnString = new String(strings);
		return returnString.trim();
	}
	public static void writeByEncoding(File file, String s, String encoding)
			throws IOException {
		if (file.getParent() != null)
			mkdirs(file.getParent());
		OutputStreamWriter bw = new OutputStreamWriter(new FileOutputStream(
				file), encoding);
		bw.flush();
		bw.write(s);
		bw.close();
	}
	public static void copyFile(File source, File destination) {
		if (!(source.exists())) {
			return;
		}
		if ((destination.getParentFile() != null)
				&& (!(destination.getParentFile().exists()))) {
			destination.getParentFile().mkdirs();
		}
		try {
			FileInputStream fis = new FileInputStream(source);
			FileOutputStream fos = new FileOutputStream(destination);
			byte[] buffer = new byte[4096];
			int n = 0;
			while ((n = fis.read(buffer)) != -1) {
				fos.write(buffer, 0, n);
			}
			fis.close();
			fos.close();
		} catch (Exception e) {
		}
	}
	public static void copyFile(String sourceFileName,
			String destinationFileName) {
		copyFile(new File(sourceFileName), new File(destinationFileName));
	}
	public static boolean fileExisted(String strFullPath) {
		if (strFullPath == null) {
			return false;
		}
		return new File(strFullPath).exists();
	}
	public static List getChildFileByExtension(File fileFolder, String extension) {
		List listFile = new ArrayList();
		if (fileFolder.isDirectory()) {
			File[] files = fileFolder.listFiles();
			for (int i = 0; i < files.length; ++i) {
				listFile.addAll(getChildFileByExtension(files[i], extension));
			}
		} else if (fileFolder.getName().endsWith("." + extension)) {
			listFile.add(fileFolder.getAbsolutePath());
		}
		return listFile;
	}
	public static List getChildFileByExtension(String fileFolder,
			String extension) {
		return getChildFileByExtension(new File(fileFolder), extension);
	}
}
